package com.im.partchat.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.bumptech.glide.Glide;
import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.topic.MenuDialog;
import com.im.partchat.topic.SureOrCancelDialog;
import com.im.partchat.topic.TopicDetailActivity;
import com.im.partchat.topic.bean.CommentBean;
import com.im.partchat.topic.bean.TopicBean;
import com.im.partchat.topic.manager.FaceManager;
import com.im.partchat.topic.widget.PictureGridLayout;
import com.im.partchat.topic.widget.PraiseTextView;
import com.im.partchat.topic.widget.SpannableTextView;

import java.util.List;


public class TopicAdapter extends BaseAdapter {
	private List<TopicBean> list;
	private LayoutInflater mInflater;
	private Activity mContext;
	private FaceManager faceManager;
	private String userId;
	private TopicBeanDeleteListener onTopicBeanDeleteListener;


	public TopicAdapter(Activity mContext, List<TopicBean> list) {
		this.list = list;
		this.mInflater = LayoutInflater.from(mContext);
		this.mContext = mContext;
		this.userId = App.getUserBean().getId();
		this.faceManager = App.getFaceManager();
	}
	
	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_topic, null);
			holder.avator = (ImageView) convertView.findViewById(R.id.avator);
			holder.header_name = (TextView) convertView.findViewById(R.id.header_name);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.bottom_textview = (SpannableTextView) convertView.findViewById(R.id.bottom_textview);
			holder.header_time = (TextView) convertView.findViewById(R.id.header_time);
			holder.header_location = (TextView) convertView.findViewById(R.id.header_location);
			holder.zan_count_btn = (PraiseTextView) convertView
					.findViewById(R.id.zan_count_btn);
			holder.review_count_btn = (TextView) convertView
					.findViewById(R.id.review_count_btn);
			holder.pictureGridLayout = (PictureGridLayout) convertView
					.findViewById(R.id.pictureGridLayout);
			holder.delete_btn = (TextView) convertView
					.findViewById(R.id.delete_btn);
			holder.bottom_praise_ll = convertView.findViewById(R.id.bottom_praise_ll);
			holder.praise_count_textview = (TextView) convertView
					.findViewById(R.id.praise_count_textview);
			
			holder.praise_ll =convertView.findViewById(R.id.praise_ll);
			holder.comment_ll =convertView.findViewById(R.id.comment_ll);
			holder.share_ll =convertView.findViewById(R.id.share_ll);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
	
		final TopicBean bean = list.get(position);
        Glide.with(App.getApplication())
                .load(bean.getMemberAvatar().findSmallUrl())
                .error(R.drawable.rc_image_default)
                .into(holder.avator);

		holder.content.setText(faceManager.
				convertNormalStringToSpannableString(mContext,bean.getContent()),
				BufferType.SPANNABLE);
		FaceManager.extractMention2Link(holder.content);

		holder.content.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				MenuDialog followDialog = new MenuDialog(mContext,
						new MenuDialog.ButtonClick() {

							@Override
							public void onSureButtonClick() {
								// TODO Auto-generated method stub
								ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
								clipboard.setText(bean.getContent());
							}
						});
				followDialog.show();
				return true;
			}
		});
		
		holder.header_location.setText(bean.getDistance() !=null?bean.getDistanceString():bean.getAddress());
		holder.header_name.setText(bean.getMemberName());
		holder.header_time.setText(bean.getTimeString());
		holder.bottom_textview.setPraiseText(bean.getPraiseMembers(), new SpannableTextView.MemberClickListener() {
			
			@Override
			public void onMemberClick(CommentBean commentBean) {
				// TODO Auto-generated method stub
			}
		});
		holder.bottom_praise_ll.setVisibility(bean.getPraiseMembers().isEmpty()?View.GONE:View.VISIBLE);
		holder.review_count_btn.setText("评论  "+bean.getCommentCount());
		
		holder.pictureGridLayout.setPictures(mContext, bean.getPicture(), bean);
		
		holder.praise_count_textview.setText(bean.getPraiseCount()+"人点赞");
		
		holder.zan_count_btn.setPraiseState(mContext, bean);
		holder.praise_ll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PraiseTextView tv =  (PraiseTextView) v.findViewById(R.id.zan_count_btn);
				tv.clickPraise(mContext, bean);
			}
		});
		
		holder.comment_ll.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.setClass(mContext, TopicDetailActivity.class);
				i.putExtra("TopicBean", bean);
				i.putExtra("position", 1);
				mContext.startActivityForResult(i, 0);
			}
		});
		
		holder.avator.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});
		holder.delete_btn.setVisibility(userId.equals(bean.getMemberId())?View.VISIBLE:View.GONE);
		holder.delete_btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SureOrCancelDialog followDialog = new SureOrCancelDialog(
						mContext, "删除掉该条动态", "好",
						new SureOrCancelDialog.SureButtonClick() {

							@Override
							public void onSureButtonClick() {
								// TODO Auto-generated method stub
								if (onTopicBeanDeleteListener!=null) {
									onTopicBeanDeleteListener.onTopicBeanDeleteClick(bean);
								}
							}
						});
				followDialog.show();
			}
		});
		holder.content.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.setClass(mContext, TopicDetailActivity.class);
				i.putExtra("TopicBean", bean);
				i.putExtra("position", 0);
				mContext.startActivityForResult(i, 0);
			}
		});
		convertView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.setClass(mContext, TopicDetailActivity.class);
				i.putExtra("TopicBean", bean);
				i.putExtra("position", 0);
				mContext.startActivityForResult(i, 0);
			}
		});
		return convertView;
	}

	public static class ViewHolder {
		private TextView content, header_name, header_time,praise_count_textview,header_location;
		private PraiseTextView zan_count_btn;
		private SpannableTextView bottom_textview;
		private ImageView avator;
		private PictureGridLayout pictureGridLayout;
		private TextView review_count_btn,delete_btn;
		private View bottom_praise_ll, praise_ll,comment_ll,share_ll;
	}
	
	public interface TopicBeanDeleteListener{
		public void onTopicBeanDeleteClick(TopicBean topicBean);
	}
	
	public void setOnTopicBeanDeleteListener(TopicBeanDeleteListener onTopicBeanDeleteListener) {
		this.onTopicBeanDeleteListener = onTopicBeanDeleteListener;
	}
	
	public TopicBeanDeleteListener getOnTopicBeanDeleteListener() {
		return onTopicBeanDeleteListener;
	}

}

